/****** Object:  Stored Procedure [dbo].spDonViTinhInsert Script Date: 6/11/2012 ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[spDonViTinhInsert]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[spDonViTinhInsert]
GO

/******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:					Description:
**		--------	--------				-----------------------------------
**		12/06/2012	SongTM					Innit created
*******************************************************************************/

CREATE PROCEDURE [dbo].[spDonViTinhInsert]
(
	@MaDonVi nvarchar(6) output,
	@TenDonVi nvarchar(20),
	@GhiChu nvarchar(100)
)
WITH ENCRYPTION

AS

SET NOCOUNT ON
if not exists (select * from [tbl_DonViTinh])
	Set @MaDonVi=1
else
	Set @MaDonVi=(select RIGHT(MAX([MaDonVi]),3) from [tbl_DonViTinh])+1
Set @MaDonVi='UNT'+REPLICATE('0',3-LEN(@MaDonVi))+@MaDonVi  
INSERT INTO [tbl_DonViTinh]
(
	[MaDonVi],
	[TenDonVi],
	[GhiChu]
)
VALUES
(
	@MaDonVi,
	@TenDonVi,
	@GhiChu
)
GO

--GRANT EXECUTE ON [dbo].[spDonViTinhInsert] TO [sa]
--GO
